<template>
	<view class="updatePassword">

		<view class="radioBox">
			<u-radio-group v-model="radiovalue1" placement="row" @change="groupChange">
				<u-radio :customStyle="{marginBottom: '8px',marginRight: '16px'}" v-for="(item, index) in radiolist1"
					:key="index" :label="item.name" :name="item.name" @change="radioChange" activeColor="#439B05">
				</u-radio>
			</u-radio-group>
		</view>

		<view v-if="radiovalue1 == '手机号验证码'" class="">

			<view class="titleBox">
				<image src="../../static/image/c1.png" style="width: 32rpx;height: 32rpx;" mode=""></image>
				<view class="lable">
					手机号
				</view>
			</view>

			<view class="inputBox">
				<view class="innerBox">

					<view class="" @click="show = true">
						+{{phoneShow}}
					</view>

					<image src="../../static/image/down.png" style="width: 40rpx;height: 40rpx;margin-left: 14rpx;"
						mode="">
					</image>

					<view class="shu"></view>

					<image src="../../static/image/phone.png" mode="" style="width: 48rpx;height: 48rpx;"></image>

					<input type="number" class="inputitem" v-model="mobile" placeholder="请输入手机号" />

				</view>
			</view>
		</view>

		<view v-else class="">
			<view class="titleBox">
				<image src="../../static/image/c1.png" style="width: 32rpx;height: 32rpx;" mode=""></image>
				<view class="lable">
					邮箱
				</view>
			</view>
			<view class="inputBox">
				<input type="text" v-model="email" placeholder="请输入邮箱号" />
			</view>
		</view>


		<view class="titleBox">
			<image src="../../static/image/c2.png" style="width: 32rpx;height: 32rpx;" mode=""></image>
			<view class="lable">
				验证码
			</view>
		</view>
		<view class="inputBox">
			<input type="text" v-model="code" placeholder="请输入您的验证码" />
			<view v-if="codeFlag" class="yzm" @click="handleSendcode">
				发送验证码
			</view>
			<view v-else class="yzm">
				{{time}}s
			</view>
		</view>

		<view class="titleBox">
			<image src="../../static/image/c3.png" style="width: 32rpx;height: 32rpx;" mode=""></image>
			<view class="lable">
				密码
			</view>
		</view>
		<view class="inputBox">
			<input type="text" v-model="password" placeholder="请输入新的登录密码" />
		</view>

		<view class="titleBox">
			<image src="../../static/image/c3.png" style="width: 32rpx;height: 32rpx;" mode=""></image>
			<view class="lable">
				确认密码
			</view>
		</view>
		<view class="inputBox">
			<input type="text" v-model="password1" placeholder="请再次输入登录密码" />
		</view>

		<view class="savebtn" @click="handleUpdate">
			确认修改
		</view>


		<u-picker :show="show" :columns="columns" @cancel="show = false" @confirm="handlePhone"></u-picker>

	</view>
</template>

<script>
	import {
		editpassword,
		sendsms,
		phoneisexist,
		sendEamil
	} from "@/api/api.js"
	export default {
		data() {
			return {
				password: "", //新密码
				password1: "", //确认密码
				code: "", //验证码
				codeFlag: true,
				time: 60,
				mobile: "",
				columns: [
					['63', '65']
				],
				show: false,
				phoneShow: "63",
				radiovalue1: '手机号验证码',
				radiolist1: [{
						name: '手机号验证码',
						disabled: false
					},
					{
						name: '邮箱验证码',
						disabled: false
					},
				],
				email: "",
			}
		},
		onLoad() {

		},
		methods: {
			groupChange(n) {
				console.log('groupChange', n, this.radiovalue1);

			},
			radioChange(n) {
				console.log('radioChange', n);
			},

			handlePhone(e) {
				this.show = false
				this.phoneShow = e.value[0]
			},
			//发送验证码
			async handleSendcode() {
				if (this.radiovalue1 == '手机号验证码') {
					if (this.mobile == "") {
						uni.showToast({
							title: '请输入手机号！',
							icon: "none",
							duration: 2000
						});
						return
					}
					let codeRes = await phoneisexist({
						mobile: this.phoneShow + this.mobile
					})
					if (codeRes.data == 1) {
						const that = this
						let res = await sendsms({
							mobile: that.phoneShow + that.mobile
						})
						if (res.code == 1000) {
							uni.showToast({
								title: res.message,
								icon: "none",
								duration: 1500
							});
							that.codeFlag = false
							const interval = setInterval(function() {
								if ((that.time--) <= 0) {
									that.time = 60
									that.codeFlag = true
									clearInterval(interval)
								}
							}, 1000)
						} else {
							uni.showToast({
								title: res.message,
								icon: "none",
								duration: 2000
							});
						}
					} else {
						uni.showToast({
							title: '该账号未在平台下注册',
							icon: "none",
							duration: 2000
						});
					}
				} else {
					var reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
					if (!reg.test(this.email)) {
						uni.showToast({
							title: '请输入正确的邮箱号！',
							icon: "none",
							duration: 2000
						});
						return
					}
					const that = this
					let res = await sendEamil({
						email: that.email
					})
					if (res.code == 1000) {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 1500
						});
						that.codeFlag = false
						const interval = setInterval(function() {
							if ((that.time--) <= 0) {
								that.time = 60
								that.codeFlag = true
								clearInterval(interval)
							}
						}, 1000)
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
							duration: 2000
						});
					}
				}

			},
			async handleUpdate() {
				if (this.code == "") {
					uni.showToast({
						title: '请输入验证码！',
						icon: "none",
						duration: 2000
					});
					return
				}
				if (this.password == "") {
					uni.showToast({
						title: '请输入新密码！',
						icon: "none",
						duration: 2000
					});
					return
				}
				if (this.password1 == "") {
					uni.showToast({
						title: '请输入确认密码！',
						icon: "none",
						duration: 2000
					});
					return
				}
				if (this.password != this.password1) {
					uni.showToast({
						title: '请确认密码是否一致!',
						icon: "none",
						duration: 2000
					});
					return
				}
				let data = {
					password: this.password,
					code: this.code,

				}
				if (this.radiovalue1 == '手机号验证码') {
					data.is_mobile = '1',
						data.mobile = this.phoneShow + this.mobile
				} else {
					data.is_mobile = '2',
						data.email = this.email
				}
				// console.log("data", data)
				let res = await editpassword(data)
				if (res.code == 1000) {
					uni.showToast({
						title: '修改成功，请重新登录!',
						icon: "none",
						duration: 2000
					});
					setTimeout(() => {
						uni.reLaunch({
							url: "/pages/login/index"
						})
					}, 1500)
				} else {
					uni.showToast({
						title: res.message,
						icon: "none",
						duration: 2000
					});
				}

			},
		}
	}
</script>

<style scoped lang="scss">
	.savebtn {
		width: 670rpx;
		height: 80rpx;
		background: #439B05;
		border-radius: 100rpx;
		font-size: 32rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		color: #FFFFFF;
		line-height: 80rpx;
		text-align: center;
		margin-top: 80rpx;
	}

	.updatePassword {
		width: 100%;
		height: 100%;
		background-color: #ffffff;
		padding: 30rpx 40rpx;
		box-sizing: border-box;

		.titleBox {
			width: 100%;
			padding: 20rpx;
			display: flex;
			align-items: center;
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color: #333333;
			line-height: 33rpx;

			.lable {
				margin-left: 10rpx;
			}
		}

		.inputBox {
			width: 100%;
			height: 100rpx;
			background: #F5F5F5;
			font-size: 32rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color: #999999;
			line-height: 38rpx;
			padding: 28rpx 42rpx;
			box-sizing: border-box;
			margin-bottom: 20rpx;
			display: flex;
			align-items: center;
			position: relative;

			.yzm {
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				color: #439B05;
				line-height: 33rpx;
				position: absolute;
				right: 30rpx;
			}

			.innerBox {
				width: 100%;
				height: 104rpx;
				background: #F5F5F5;
				border-radius: 8rpx 8rpx 8rpx 8rpx;
				padding: 28rpx 20rpx;
				box-sizing: border-box;
				display: flex;
				align-items: center;
				position: relative;

				.inputitem {
					width: 402rpx;
					padding-left: 20rpx;
					box-sizing: border-box;
				}

				.yzm {
					font-size: 28rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					color: #439B05;
					line-height: 33rpx;
					position: absolute;
					right: 40rpx;
				}

				.shu {
					width: 2rpx;
					height: 56rpx;
					background-color: #CCCCCC;
					margin: 0 30rpx;
				}
			}
		}
	}
</style>